package com.yiwei.hdb.activity;

import android.app.Notification;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.util.Log;

import com.bugtags.library.Bugtags;
import com.yiwei.hdb.R;
import com.yiwei.hdb.constant.Const;
import com.mob.MobApplication;
//import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.analytics.MobclickAgent;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;

import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;
import kangcheng.com.lmzx_android_sdk_v10.commom.sdk.LmzxSDK;
import kangcheng.com.lmzx_android_sdk_v10.commom.sdk.LmzxSdkImpl;

/**
 * Created by MasterDong on 16/9/5.
 */
public class Application extends MobApplication {

    private static Application instance;
    private LmzxSdkImpl sdk;
//    public Typeface typeface;

    public static Application getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
//f1bcc3b335腾讯BUG检测id
//        CrashReport.initCrashReport(getApplicationContext(), "f1bcc3b335", false);
        JPushInterface.setDebugMode(false);
        JPushInterface.init(this);
        JPushInterface.resumePush(this);

        MobclickAgent.openActivityDurationTrack(false); //禁止默认页面统计
        // CrashReport.testJavaCrash();
//        typeface = Typeface.createFromAsset(getAssets(),
//                "fonts/Futura LT Book.ttf");
        sdk = LmzxSdkImpl.getInstance();
        if (Const.IS_DEBUG) {
            sdk.setEnv(this, "https://api.limuzhengxin.com/api/gateway");
//            sdk.setEnv(this, "https://t.limuzhengxin.cn:3443/api/gateway");
            Bugtags.start("8875f5b849e01b4010c4bb14ebce8aaf", this, Bugtags.BTGInvocationEventBubble);
//            sdk.setEnv(BaseContext, "https://t.limuzhengxin.cn/api/gateway");
        } else {
            sdk.setEnv(this, "https://api.limuzhengxin.com/api/gateway");
//            Bugtags.start("8875f5b849e01b4010c4bb14ebce8aaf", this, Bugtags.BTGInvocationEventBubble);
        }
//        Log.i("dasdadsdasdas", sHA1(this));

    }

    public void LmsdkInit(String apikey, String uid) {
        sdk.init(this, apikey, uid, Const.SERVER_API_URL + "/callback/limu");//apikey 必须的
    }

    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }


    public static String sHA1(Context context) {
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), PackageManager.GET_SIGNATURES);
            byte[] cert = info.signatures[0].toByteArray();
            MessageDigest md = MessageDigest.getInstance("SHA1");
            byte[] publicKey = md.digest(cert);
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < publicKey.length; i++) {
                String appendString = Integer.toHexString(0xFF & publicKey[i])
                        .toUpperCase(Locale.US);
                if (appendString.length() == 1)
                    hexString.append("0");
                hexString.append(appendString);
                hexString.append(":");
            }
            String result = hexString.toString();
            return result.substring(0, result.length() - 1);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}
